<template>
  <div style="width:200px;">
    <div class="upload-card" v-if="uploadValue">
      <div>
        <img :src="baseUrl+uploadValue" />
        <div class="upload-card-cover">
          <Icon type="ios-eye-outline" @click="handleView(uploadValue)"></Icon>
          <Icon type="ios-trash-outline" @click="handleRemove(uploadValue)"></Icon>
        </div>
      </div>
    </div>
    <Upload
      v-else
      ref="upload"
      :multiple="multiple"
      :show-upload-list="false"
      :on-success="handleSuccess"
      :on-error="handleError"
      :format="['jpg','jpeg','png','gif']"
      :max-size="5120"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      type="drag"
      :action="baseUrl+uploadFileUrl"
      :headers="token"
      style="display: inline-block;"
    >
      <div style="width: 199px;height:118px;line-height: 118px;">
        <span>{{cardName}}</span>
        <Icon type="md-camera" size="20"></Icon>
      </div>
    </Upload>

    <Modal title="图片预览" v-model="viewImage" :styles="{top: '30px'}" draggable>
      <img :src="imgUrl" alt="无效的图片链接" style="width: 100%;margin: 0 auto;display: block;" />
      <div slot="footer">
        <Button @click="viewImage=false">关闭</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import { BASE_URL } from "../../../libs/axios";
export default {
  name: "uploadPicCard",
  props: {
    value: Object,
    multiple: {
      type: Boolean,
      default: false
    },
    limit: {
      type: Number,
      default: 10
    },
    uploadFileUrl: {
      type: String
    },
    cardName: {
      type: String
    },
    upload: {
      type: String
    }
  },
  data() {
    return {
      token: {},
      viewImage: false,
      imgUrl: "",
      uploadValue:'',
      baseUrl: BASE_URL
    };
  },
    watch: {
    upload(curVal, oldVal) {
      this.uploadValue = curVal;
    }
  },
  methods: {
    init() {
      this.token = {
        token: this.getStore("token")
      };
    },
    handleView(imgUrl) {
      this.imgUrl = this.baseUrl + imgUrl;
      this.viewImage = true;
    },
    handleRemove(file) {
      this.upload = "";
      this.returnValue();
    },
    handleSuccess(res, file) {
      if (res.code == 0) {
         this.uploadValue = res.data;
        // 返回组件值
        this.returnValue();
      } else {
        this.$Message.error(res.msg);
      }
    },
    handleError(error, file, fileList) {
      this.$Message.error(error.toString());
    },
    handleFormatError(file) {
      this.$Notice.warning({
        title: "不支持的文件格式",
        desc:
          "所选文件‘ " +
          file.name +
          " ’格式不正确, 请选择 .jpg .jpeg .png .gif图片格式文件"
      });
    },
    handleMaxSize(file) {
      this.$Notice.warning({
        title: "文件大小过大",
        desc: "所选文件‘ " + file.name + " ’大小过大, 不得超过 5M."
      });
    },
    handleBeforeUpload() {
      // if(this.multiple&&this.uploadList.length>=this.limit){
      //   this.$Message.warning("最多只能上传"+this.limit+"张图片");
      //   return false;
      // }
      return true;
    },
    returnValue() {
      if (!this.uploadValue) {
        this.$emit("on-change", "");

        return;
      }
      this.$emit("on-change", this.uploadValue);
    },
    setData(v) {
      this.returnValue();
    }
  },
  mounted() {
    this.init();
  }
};
</script>

<style  lang="less">
.upload-card {
  display: inline-block;
  width: 200px;
  height: 120px;
  text-align: center;
  line-height: 60px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 5px;
}
.upload-card img {
  width: 100%;
  height: 100%;
}
.upload-card-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.upload-card:hover .upload-card-cover {
  display: block;
}
.upload-card-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>

